package com.plian.system.service.im.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.im.FundInvestedProject;
import com.plian.system.mapper.im.FundInvestedProjectMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.im.IFundInvestedProjectService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author
 * @since 2019-09-27
 */
@Slf4j
@Service
@AllArgsConstructor
public class FundInvestedProjectImpl extends BaseServiceImpl<FundInvestedProjectMapper, FundInvestedProject> implements IFundInvestedProjectService {


	@Override
	public boolean saveOrUpdate(FundInvestedProject fundInvestedProject) {
		return super.saveOrUpdate(fundInvestedProject);
	}

	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public boolean deleteLogic(List<Long> ids) {
		for (Long id : ids) {
			FundInvestedProject fundInvestedProject = getById(id);
			if (fundInvestedProject == null) {
				return false;
			}
		}
		return super.deleteLogic(ids);
	}

	@Override
	public boolean deleteByParentId(List<Long> ids) {
		LambdaQueryWrapper<FundInvestedProject> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(FundInvestedProject::getFundProjectId, ids);
		List<FundInvestedProject> fundInvestedProjects = list(queryWrapper);
		if (CollectionUtil.isNotEmpty(fundInvestedProjects)){
			return deleteLogic(fundInvestedProjects.stream().map(FundInvestedProject::getId).collect(Collectors.toList()));
		}
		return true;
	}
}
